package com.gr.staffpm.pages.projects;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.gr.staffpm.charts.CreatedVsResolvedChartGenerator;
import com.gr.staffpm.charts.JFreeChartImageWithToolTip;
import com.gr.staffpm.datatypes.Project;
import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.pages.tasks.ViewTask;
import com.gr.staffpm.projects.service.ProjectService;
import com.gr.staffpm.tasks.service.SortableTaskDataProvider;


public class SummaryTabPanel extends Panel {

    @SpringBean(name = "projectService")
    private ProjectService projectService;
    private final Project project;
    private static final long serialVersionUID = 1L;

    /**
     * Constructor
     * 
     * @param id the component id
     * @param p the {@link Project} to which we are viewing
     */
    public SummaryTabPanel(String id, Project p) {
        super(id);

        this.project = p;
        final SimpleDateFormat date = new SimpleDateFormat("dd MMM yyyy");

        //Summary Module
        add(new Label("projectLead", p.getOwner().getFirstName() + " " + p.getOwner().getLastName()));
        add(new Label("projectStart", date.format(p.getStartDate())));
        add(new Label("projectDesc", p.getDescription()));

        //Tasks Due Module
        SortableTaskDataProvider dp = new SortableTaskDataProvider(projectService, project);
        dp.setSort(Task.DUE_DATE, true);
        final DataView<Task> dataView = new DataView<Task>("tasksDue", dp) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(Item<Task> item) {
                final Task task = item.getModelObject();

                Link<Task> taskLink = new Link<Task>("taskLink", item.getModel()) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick() {
                        setResponsePage(new ViewTask(new PageParameters("id=" + String.valueOf(task.getTaskId()))));
                    }

                };

                item.add(taskLink.add(new Label("name", task.getName())));
                item.add(new Label("due", date.format(task.getDueDate())));
            }
        };
        dataView.setItemsPerPage(5);

        add(new OrderByBorder("orderByTaskName", Task.NAME, dp));
        add(new OrderByBorder("orderByDueDate", Task.DUE_DATE, dp));

        add(dataView);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -30);
        Date thirtyDaysAgo = new Date(cal.getTime().getTime());

        List<Task> tasksCreated = projectService.getTasksCreated(project, thirtyDaysAgo, new Date(Calendar.getInstance().getTime().getTime()));
        List<Task> tasksResolved = projectService.getTasksResolved(project, thirtyDaysAgo, new Date(Calendar.getInstance().getTime().getTime()));

        CreatedVsResolvedChartGenerator chartGenerator = new CreatedVsResolvedChartGenerator(30, tasksCreated, tasksResolved);

        add(new JFreeChartImageWithToolTip("chart", chartGenerator.getChartModel(), "tooltip", 520, 400));

    }

}
